/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package boundary;

import entity.Group;
import entity.User;
import java.security.Principal;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author MONESI
 */
@Stateless
public class UserManager {

    @PersistenceContext
    EntityManager em;
    
    @Inject
    Principal principal;
    
    List<User> utenti;
    
    @EJB
    UserFacade uf;

    public void save(User user) {
        user.setGroupName(Group.USERS);
        em.persist(user);
        
    }

    public void unregister() {
        em.remove(getLoggedUser());
    }

    public User getLoggedUser() {
        return em.find(User.class, principal.getName());
    }
    
   public List<User> utenti(){
       return em.createQuery("SELECT e FROM USERS E").getResultList();
   }
   
    
    public void setPrivacyPrivate(String loggedUser) {
        utenti = (List) uf.findAll();
        boolean trovato=false;
        
        for(int i=0;i<utenti.size() && !trovato;i++){
            if(utenti.get(i).getEmail().equals(loggedUser)){
                trovato=true;
                utenti.get(i).setCalendarPrivacy("Private");
                uf.edit(utenti.get(i));
  
            }
            
        }
                
    }
    public void setPrivacyPublic(String loggedUser) {
        utenti = (List) uf.findAll();
                boolean trovato=false;

        for(int i=0;i<utenti.size() && !trovato;i++){
            if(utenti.get(i).getEmail().equals(loggedUser)){
                trovato=true;
                utenti.get(i).setCalendarPrivacy("Public");
                uf.edit(utenti.get(i));
  
            }
            
        }
                
    }
    
}

